package com.mockapi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mockapi.common.CommonResult;
import com.mockapi.dto.EventRequest;
import com.mockapi.entity.Event;
import com.mockapi.entity.EventTrigger;
import com.mockapi.entity.Route;
import com.mockapi.service.EventService;
import com.mockapi.service.RouteService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 事件管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/events")
@RequiredArgsConstructor
public class EventController {

    private final EventService eventService;
    private final RouteService routeService;

    /**
     * 分页查询事件
     */
    @GetMapping
    public CommonResult<Page<Event>> getEventPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String eventType,
            @RequestParam(required = false) String triggerType) {
        
        Page<Event> result = eventService.getEventPage(page, size, name, eventType, triggerType);
        return CommonResult.success(result);
    }

    /**
     * 根据ID获取事件
     */
    @GetMapping("/{id}")
    public CommonResult<Event> getEvent(@PathVariable Long id) {
        Event event = eventService.getById(id);
        if (event == null) {
            return CommonResult.error("事件不存在");
        }
        return CommonResult.success(event);
    }

    /**
     * 创建事件
     */
    @PostMapping
    public CommonResult<Event> createEvent(@Valid @RequestBody EventRequest request) {
        Event event = eventService.createEvent(request);
        return CommonResult.success(event);
    }

    /**
     * 更新事件
     */
    @PutMapping("/{id}")
    public CommonResult<Event> updateEvent(@PathVariable Long id, @Valid @RequestBody EventRequest request) {
        Event event = eventService.updateEvent(id, request);
        return CommonResult.success(event);
    }

    /**
     * 删除事件
     */
    @DeleteMapping("/{id}")
    public CommonResult<Void> deleteEvent(@PathVariable Long id) {
        eventService.deleteEvent(id);
        return CommonResult.success();
    }

    /**
     * 启用/禁用事件
     */
    @PutMapping("/{id}/toggle")
    public CommonResult<Void> toggleEvent(@PathVariable Long id, @RequestParam boolean enabled) {
        eventService.toggleEvent(id, enabled);
        return CommonResult.success();
    }

    /**
     * 手动触发事件
     */
    @PostMapping("/{id}/trigger")
    public CommonResult<EventTrigger> triggerEvent(@PathVariable Long id, @RequestBody(required = false) Map<String, Object> context) {
        if (context == null) {
            context = new HashMap<>();
        }
        Event event = eventService.getById(id);
        EventTrigger trigger = eventService.triggerEvent(event, context);
        return CommonResult.success(trigger);
    }

    /**
     * 获取事件触发记录
     */
    @GetMapping("/{id}/triggers")
    public CommonResult<Page<EventTrigger>> getEventTriggers(
            @PathVariable Long id,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        
        Page<EventTrigger> result = eventService.getEventTriggerPage(id, page, size);
        return CommonResult.success(result);
    }

    /**
     * 获取事件类型列表
     */
    @GetMapping("/types")
    public CommonResult<Map<String, String>> getEventTypes() {
        Map<String, String> types = new HashMap<>();
        types.put("HTTP", "HTTP调用");
        types.put("MESSAGE", "消息发送");
        return CommonResult.success(types);
    }

    /**
     * 获取触发类型列表
     */
    @GetMapping("/trigger-types")
    public CommonResult<Map<String, String>> getTriggerTypes() {
        Map<String, String> types = new HashMap<>();
        types.put("MOCK_CALL", "Mock调用后触发");
        return CommonResult.success(types);
    }

    /**
     * 获取路由列表（用于事件关联）
     */
    @GetMapping("/routes")
    public CommonResult<List<Route>> getRoutes() {
        List<Route> routes = routeService.getAllRoutes();
        return CommonResult.success(routes);
    }
} 